//============================================================================================
/**
 * @file	yesno_poketch.c
 * @brief	フィールドＹＥＳＮＯ選択下画面インターフェース
 * @author	AkitoMori GAME FREAK inc.
 * @date	08.12.20
 */
//============================================================================================

#include "common.h"
#include "tcb.h"
#include "system\arc_util.h"
#include "system/clact_tool.h"
#include "system/lib_pack.h"
#include "system/snd_tool.h"
#include "system/fontoam.h"
#include "system/fontproc.h"
#include "system/touch_subwindow.h"
#include "system/pmfprint.h"
#include "gflib/touchpanel.h"
#include "system/window.h"
#include "field/poketch.h"
#include "field/syswork.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_fieldmenu.h"
#include "system\wordset.h"
#include "system\msgdata_util.h"
#include "field/sysflag.h"
#include "field/fld_pmtel.h"
#include "field/fld_bmp.h"
#include "field/field_poketch.h"

#include "poketch_arc.h"
#include "msgdata/msg_common_scr.h"
#include "msgdata/msg_ev_win.h"
#include "application/subselect.naix"

#define MENU_OAM_NUM		( 14 )
#define SHORTCUT_OAM_NUM	( 3 )
#define CURSOR_OAM_NUM		( 4 )
#define MENU_ALL_OAM_NUM	( MENU_OAM_NUM + SHORTCUT_OAM_NUM + CURSOR_OAM_NUM )

#define MENU_MAX			(  9 )

// CLACTで定義しているセルが大きすぎてサブ画面に影響がでてしまうので離してみる
#define SUBLCD_MENU_SUB_ACTOR_DOT		( 256 )
#define SUBLCD_MENU_SUB_ACTOR_DISTANCE 	(SUBLCD_MENU_SUB_ACTOR_DOT*FX32_ONE)

// CellActorに処理させるリソースマネージャの種類の数（＝マルチセル・マルチセルアニメは使用しない）
#define CLACT_RESOURCE_TYPE_NUM		(  4 )

// システムメニューのセルアクターが読み込むキャラ・パレット（セットで）の総数
#define CLACT_RESOURCE_CHARA_NUM	( 10 )

#define SUBLCD_OAM_NUM			( 13 )
#define CLACT_RESOURCE_TBL_NUM	(  2 )
#define REPORT_END_WAIT			( 20 )

#define HEAPID_SUBLCD_MENU		( HEAPID_POKETCH_APP )
#define ARC_SUBLCDMENU_GRA		( ARC_POKETCH_IMG )


#define YESNO_CONTINUE	( 0 )
#define YESNO_END		( 1 )

enum{
	SEQ_START=0,
	SEQ_WAIT,
	SEQ_END,
	SEQ_YESNO_START,		// 3
	SEQ_YESNO_WAIT,
	SEQ_YESNO_ANIME_WAIT,
	SEQ_YESNO_END,
	SEQ_SELECTLIST_START,	// 7
	SEQ_SELECTLIST_WAIT,
	SEQ_SELECTLIST_ANIME_WAIT,
	SEQ_SELECTLIST_END,
	SEQ_BREAK,				// 11 強制終了
};


//------------------------------------------------------------------
/// 構造体定義
//------------------------------------------------------------------
typedef struct {
	SUBLCD_PARAM	param;
	int			wait;
	int     	screen;
	GF_BGL_INI	*bgl;
	TCB_PTR 	tcb;

	u16         *panel_trg;
	FIELDSYS_WORK    *fsys;

	GF_BGL_BMPWIN			YesnoBmpWin[2];			// 選択中の項目を説明する文章を表示するBMPWIN領域
	int						MenuNameOn;			// MenuBmpを表示しているか。

	MSGDATA_MANAGER 		*msgman;
	WORDSET					*WordSet;			// メッセージ展開用ワークマネージャー

	GF_BGL_BMPWIN			BmpMenuListWin[28];	// 選択メニュー用のBMPWIN
	int						bmpmenulist_max;

	// セルアクター関連
	CLACT_SET_PTR 			clactSet;							// セルアクターセット
	CLACT_U_EASYRENDER_DATA	renddata;							// 簡易レンダーデータ
	CLACT_U_RES_MANAGER_PTR	resMan[CLACT_RESOURCE_TYPE_NUM];	// リソースマネージャ
	CLACT_U_RES_OBJ_PTR 	resObjTbl[CLACT_RESOURCE_TYPE_NUM];	// リソースオブジェテーブル
	CLACT_HEADER			clActHeader;						// セルアクターヘッダー
	CLACT_WORK_PTR			CursorActWork;						// セルアクターワークポインタ配列

	//はい・いいえタッチパネル関連
	STRBUF					*savestr;
	int						msg_index;
	int						pos;
	void					*TimeWaitIconPtr;	//

	// コールバック
	pYNSublcdFunc	callback_func;
	void * callback_work;

}YESNO_SUBLCD_WORK;

//------------------------------------------------------------------
/// プロトタイプ宣言
//------------------------------------------------------------------
static void _bg_set( YESNO_SUBLCD_WORK *ysw );
static void _clact_init( YESNO_SUBLCD_WORK *ysw );
static void _clact_set( YESNO_SUBLCD_WORK *ysw, int pos);
static void _yesno_bg_init( YESNO_SUBLCD_WORK *ysw );
static void _yesno_bg_release( YESNO_SUBLCD_WORK *ysw );
static void _selectlist_bg_init( YESNO_SUBLCD_WORK *ysw );
static void _selectlist_bg_release( YESNO_SUBLCD_WORK *ysw );


static void _sublcd_task( TCB_PTR tcb, void *wk );


static void _string_get( YESNO_SUBLCD_WORK *ysw, int flag );
static void _string_release( YESNO_SUBLCD_WORK *ysw );
static void _yesno_start( YESNO_SUBLCD_WORK *ysw );

static void _set_decide_style( KEYTOUCH_STATUS *kt_st, int type );


static int _seq_start( YESNO_SUBLCD_WORK *ysw );
static int _seq_yesno_start( YESNO_SUBLCD_WORK *ysw );
static int _seq_yesno_wait( YESNO_SUBLCD_WORK *ysw );
static int _seq_yesno_anime_wait( YESNO_SUBLCD_WORK *ysw );
static int _seq_yesno_end( YESNO_SUBLCD_WORK *ysw );
static int _seq_end( YESNO_SUBLCD_WORK *ysw );
static int _seq_wait( YESNO_SUBLCD_WORK *ysw );
static int _seq_selectlist_start( YESNO_SUBLCD_WORK *ysw );
static int _seq_selectlist_wait( YESNO_SUBLCD_WORK *ysw );
static int _seq_selectlist_anime_wait( YESNO_SUBLCD_WORK *ysw );
static int _seq_selectlist_end( YESNO_SUBLCD_WORK *ysw );
static int _seq_break( YESNO_SUBLCD_WORK *ysw );

static void bmpmenu_cursor_act_pos( YESNO_SUBLCD_WORK *ysw, int max, int pos );
static void bmpmenu_cursor_anime_start( YESNO_SUBLCD_WORK *ysw, int max );


static void ButtonMsgDraw( GF_BGL_BMPWIN *win, MSGDATA_MANAGER *msgman, int msgno );
static void cursor_act_pos( YESNO_SUBLCD_WORK *ysw, int pos );
static  int yesno_control( YESNO_SUBLCD_WORK *ysw );

static int _all_save_check( YESNO_SUBLCD_WORK *ysw );
static int move_key_next( int *pos, int max, int arrow );


// サブシーケンス定義表
static int (*sub_seq_table[])( YESNO_SUBLCD_WORK *ysw )={
	_seq_start,
	_seq_wait,
	_seq_end,
	_seq_yesno_start,
	_seq_yesno_wait,
	_seq_yesno_anime_wait,
	_seq_yesno_end,
	_seq_selectlist_start,
	_seq_selectlist_wait,
	_seq_selectlist_anime_wait,
	_seq_selectlist_end,
	_seq_break,
};



//------------------------------------------------------------------
/**
 * ポケッチ取得以前の画面構築
 *
 * @param   none
 *
 */
//------------------------------------------------------------------
TCB_PTR YesnoSublcdInit( GF_BGL_INI* bgl, u16 *sub_panel_trg, FIELDSYS_WORK *fsys, void *work )
{
	TCB_PTR       tcb;

	static const GF_BGL_BGCNT_HEADER header[] = {
		{
			0, 0, 0x800, 0,	// scrX, scrY, scrbufSize, scrbufofs,
			GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0x6800, GX_BG_CHARBASE_0x00000,
			GX_BG_EXTPLTT_01, 1, 0, 0, FALSE	// pal, pri, areaover, dmy, mosaic
		},
		{
			0, 0, 0x800, 0,	// scrX, scrY, scrbufSize, scrbufofs,
			GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0x7000, GX_BG_CHARBASE_0x00000,
			GX_BG_EXTPLTT_01, 0, 0, 0, FALSE	// pal, pri, areaover, dmy, mosaic

		},
		{
			0, 0, 0x800, 0,	// scrX, scrY, scrbufSize, scrbufofs,
			GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0x6000, GX_BG_CHARBASE_0x04000,
			GX_BG_EXTPLTT_01, 2, 0, 0, FALSE	// pal, pri, areaover, dmy, mosaic

		},
	};

	// 異様にデカいがポケッチの合計と同じにしておく
	sys_CreateHeap( HEAPID_BASE_APP, HEAPID_SUBLCD_MENU,  0x18000 );

	// BG 設定
	GXS_SetGraphicsMode(GX_BGMODE_0);

	GX_SetBankForSubBG(GX_VRAM_SUB_BG_32_H);
	GX_SetBankForSubOBJ(GX_VRAM_SUB_OBJ_16_I);
	GXS_SetOBJVRamModeChar( GX_OBJVRAMMODE_CHAR_1D_32K );
	GF_BGL_BGControlSet( bgl, GF_BGL_FRAME0_S, &header[0], GF_BGL_MODE_TEXT );
	GF_BGL_BGControlSet( bgl, GF_BGL_FRAME1_S, &header[1], GF_BGL_MODE_TEXT );
	GF_BGL_BGControlSet( bgl, GF_BGL_FRAME2_S, &header[2], GF_BGL_MODE_TEXT );
	GF_BGL_ClearCharSet( GF_BGL_FRAME0_S, 32, 0, HEAPID_FIELD );
	GF_BGL_ClearCharSet( GF_BGL_FRAME1_S, 32, 0, HEAPID_FIELD );
	GF_BGL_ClearCharSet( GF_BGL_FRAME2_S, 32, 0, HEAPID_FIELD );
	GF_BGL_ScrClear( bgl, GF_BGL_FRAME0_S );
	GF_BGL_ScrClear( bgl, GF_BGL_FRAME1_S );

	{
		YESNO_SUBLCD_WORK *ysw;
		int i;

		tcb             = PMDS_taskAdd( _sublcd_task, sizeof(YESNO_SUBLCD_WORK), 10, HEAPID_SUBLCD_MENU);
		ysw             = TCB_GetWork(tcb);
		ysw->tcb        = tcb;
		ysw->screen     = 0;
		ysw->bgl        = bgl;
		ysw->panel_trg  = sub_panel_trg;
		ysw->fsys       = fsys;
		ysw->MenuNameOn = 0;
		ysw->param.subseq     = SEQ_START;
		ysw->param.scr_work   = (u32*)work;

		ysw->callback_func = NULL;
		ysw->callback_work = NULL;

		// 外部から強制終了する場合の対策
		ysw->YesnoBmpWin[0].chrbuf = NULL;
		ysw->YesnoBmpWin[1].chrbuf = NULL;

		// タッチフォント読み込み
		FontProc_LoadFont(FONT_TOUCH,HEAPID_SUBLCD_MENU);

		// メニュー文字列取得
		_string_get( ysw, ysw->fsys->sublcd_menu_on );

		// BG転送
		_bg_set( ysw );

		// セルアクター初期化
		_clact_init( ysw );
		_clact_set( ysw, 0 );

		OS_Printf("menu_on_flag = %d\n", ysw->fsys->sublcd_menu_on);


	}

	GXS_SetVisibleWnd( GX_WNDMASK_NONE );
//	GXS_SetVisiblePlane( GX_PLANEMASK_BG0|GX_PLANEMASK_BG1|GX_PLANEMASK_BG2 );
	GF_Disp_GXS_VisibleControl(GX_PLANEMASK_BG0, VISIBLE_ON);	//OBJ面ＯＮ
	GF_Disp_GXS_VisibleControl(GX_PLANEMASK_BG1, VISIBLE_ON);	//OBJ面ＯＮ
	GF_Disp_GXS_VisibleControl(GX_PLANEMASK_BG2, VISIBLE_ON);	//OBJ面ＯＮ
	GF_Disp_GXS_VisibleControl(GX_PLANEMASK_OBJ, VISIBLE_ON);	//OBJ面ＯＮ

	// 会話ウインドウタッチ有効
	MsgPrintTouchPanelFlagSet( MSG_TP_ON );

	return tcb;
}



//==============================================================================
/**
 * @brief   終了リクエスト
 *
 * @param   bgl
 * @param   tcb
 *
 * @retval  none
 */
//==============================================================================
void YesnoSublcdQuitReq( GF_BGL_INI* bgl,  TCB_PTR tcb )
{
	int i;

	YESNO_SUBLCD_WORK *ysw = TCB_GetWork(tcb);

	OS_Printf("tcb=%08x\n",tcb);

	// セルアクターリソース解放
	// キャラ転送マネージャー破棄
	CLACT_U_CharManagerDelete( ysw->resObjTbl[CLACT_U_CHAR_RES] );

	// パレット転送マネージャー破棄
	CLACT_U_PlttManagerDelete( ysw->resObjTbl[CLACT_U_PLTT_RES] );

	// キャラ・パレット・セル・セルアニメのリソースマネージャー破棄
	for(i=0;i<CLACT_RESOURCE_TYPE_NUM;i++){
		CLACT_U_ResManagerDelete(ysw->resMan[i]);
	}

	// セルアクターセット破棄
	CLACT_DestSet(ysw->clactSet);

	// 会話ウインドウタッチ有効
	MsgPrintTouchPanelFlagSet( MSG_TP_OFF );



	// メニュー文字列解放
	_string_release( ysw );

	//タッチフォントアンロード
	FontProc_UnloadFont(FONT_TOUCH);

	// タスク消去
	PMDS_taskDel( tcb );

	GF_BGL_BGControlExit( bgl, GF_BGL_FRAME2_S );
	GF_BGL_BGControlExit( bgl, GF_BGL_FRAME1_S );
	GF_BGL_BGControlExit( bgl, GF_BGL_FRAME0_S );

	sys_DeleteHeap( HEAPID_SUBLCD_MENU );

}


//==============================================================================
/**
 * @brief   終了待ち（即終了）
 *
 * @param   bgl
 *
 * @retval  BOOL
 */
//==============================================================================
BOOL YesnoSublcdQuitWait( GF_BGL_INI* bgl )
{
	return TRUE;
}

void YesnoSublcdCallBackSet( TCB_PTR tcb, pYNSublcdFunc func, void * work )
{
	YESNO_SUBLCD_WORK * ysw = TCB_GetWork(tcb);

	ysw->callback_func = func;
	ysw->callback_work = work;
}


#define TALK_BMPWIN_CGX			(32*4)
#define TALK_WINDOW_CGX			(TALK_BMPWIN_CGX+(FLD_MSG_WIN_SX*FLD_MSG_WIN_SY))
#define TALK_WINDOW_PAL			(  5 )
#define TALK_STRING_PAL		 	(  4 )


static int debug_x = 128;
static int debug_y = 120;
//------------------------------------------------------------------
/**
 * @brief   下画面レポート
 *
 * @param   tcb
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _sublcd_task( TCB_PTR tcb, void *wk )
{
	int i;
	YESNO_SUBLCD_WORK *ysw = (YESNO_SUBLCD_WORK *)wk;

	// イベント中か？
	if(!FieldEvent_Check( ysw->fsys )){

		// 閉じた時の挙動
		if(ysw->fsys->sublcd_menu_on==2){
			ysw->fsys->sublcd_menu_on=3;

//			ReportSubLcd_EraseWindow(ysw->riw);
		}

		G2S_BlendNone();

	}else{
		// イベント中で、しかもメインメニューを開いている
//		if(	ysw->fsys->sublcd_menu_on==2 ){

			if( sub_seq_table[ysw->param.subseq](ysw)==TRUE ){
				ysw->fsys->sublcd_menu_on = 0;
//				ysw->fsys->sublcd_menu_on = 1;
				FieldSubLcdControler_ChangeApp( ysw->fsys, SUBLCD_MODE_FIELD_MENU, NULL );
			}else{
//				FieldMenuSubLcdReturn( SUBLCD_MODE_FIELD_MENU );
			}
//		}else{
			// イベント中でメインメニューを開いていない（会話やスクリプトが動作している）

//		}

	}
	CLACT_Draw( ysw->clactSet );


}

// はい・いいえ用BMPWIN定義
#define YESNO_BMP_X		( 12 )
#define YESNO_BMP_Y1	(  8 )
#define YESNO_BMP_Y2	( 14 )
#define YESNO_BMP_W		(  8 )
#define YESNO_BMP_H		(  2 )
#define YESNO_BMP_OFFSET	( 32*4 )

// 選択肢用BMP定義
#define SEL2_PANEL0_X	(  2 )
#define SEL2_PANEL0_Y	(  8 )
#define SEL2_PANEL1_X	(  2 )
#define SEL2_PANEL1_Y	( 14 )
#define SEL2_PANEL_W	( 28 )
#define SEL2_PANEL_H	(  2 )

#define SEL3_PANEL0_X	(  2 )
#define SEL3_PANEL0_Y	(  5 )
#define SEL3_PANEL1_X	(  2 )
#define SEL3_PANEL1_Y	( 11 )
#define SEL3_PANEL2_X	(  2 )
#define SEL3_PANEL2_Y	( 17 )
#define SEL3_PANEL_W	( 28 )
#define SEL3_PANEL_H	(  2 )

#define SEL4_PANEL0_X	(  2 )
#define SEL4_PANEL0_Y	(  2 )
#define SEL4_PANEL1_X	(  2 )
#define SEL4_PANEL1_Y	(  8 )
#define SEL4_PANEL2_X	(  2 )
#define SEL4_PANEL2_Y	( 14 )
#define SEL4_PANEL3_X	(  2 )
#define SEL4_PANEL3_Y	( 20 )
#define SEL4_PANEL_W	( 28 )
#define SEL4_PANEL_H	(  2 )

#define SEL5_PANEL0_X	(  2 )
#define SEL5_PANEL0_Y	(  4 )
#define SEL5_PANEL1_X	( 18 )
#define SEL5_PANEL1_Y	(  4 )
#define SEL5_PANEL2_X	(  2 )
#define SEL5_PANEL2_Y	( 10 )
#define SEL5_PANEL3_X	( 18 )
#define SEL5_PANEL3_Y	( 10 )
#define SEL5_PANEL4_X	( 18 )
#define SEL5_PANEL4_Y	( 16 )
#define SEL5_PANEL_W	( 13 )
#define SEL5_PANEL_H	(  4 )

#define SEL6_PANEL0_X	(  2 )
#define SEL6_PANEL0_Y	(  4 )
#define SEL6_PANEL1_X	( 18 )
#define SEL6_PANEL1_Y	(  4 )
#define SEL6_PANEL2_X	(  2 )
#define SEL6_PANEL2_Y	( 10 )
#define SEL6_PANEL3_X	( 18 )
#define SEL6_PANEL3_Y	( 10 )
#define SEL6_PANEL4_X	(  2 )
#define SEL6_PANEL4_Y	( 16 )
#define SEL6_PANEL5_X	( 18 )
#define SEL6_PANEL5_Y	( 16 )
#define SEL6_PANEL_W	( 12 )
#define SEL6_PANEL_H	(  4 )

#define SEL7_PANEL0_X	(  2 )
#define SEL7_PANEL0_Y	(  1 )
#define SEL7_PANEL1_X	( 18 )
#define SEL7_PANEL1_Y	(  1 )
#define SEL7_PANEL2_X	(  2 )
#define SEL7_PANEL2_Y	(  7 )
#define SEL7_PANEL3_X	( 18 )
#define SEL7_PANEL3_Y	(  7 )
#define SEL7_PANEL4_X	(  2 )
#define SEL7_PANEL4_Y	( 13 )
#define SEL7_PANEL5_X	( 18 )
#define SEL7_PANEL5_Y	( 13 )
#define SEL7_PANEL6_X	( 18 )
#define SEL7_PANEL6_Y	( 19 )
#define SEL7_PANEL_W	( 12 )
#define SEL7_PANEL_H	(  4 )

#define SEL8_PANEL0_X	(  2 )
#define SEL8_PANEL0_Y	(  1 )
#define SEL8_PANEL1_X	( 18 )
#define SEL8_PANEL1_Y	(  1 )
#define SEL8_PANEL2_X	(  2 )
#define SEL8_PANEL2_Y	(  7 )
#define SEL8_PANEL3_X	( 18 )
#define SEL8_PANEL3_Y	(  7 )
#define SEL8_PANEL4_X	(  2 )
#define SEL8_PANEL4_Y	( 13 )
#define SEL8_PANEL5_X	( 18 )
#define SEL8_PANEL5_Y	( 13 )
#define SEL8_PANEL6_X	(  2 )
#define SEL8_PANEL6_Y	( 19 )
#define SEL8_PANEL7_X	( 18 )
#define SEL8_PANEL7_Y	( 19 )
#define SEL8_PANEL_W	( 12 )
#define SEL8_PANEL_H	(  4 )


// 選択肢表示用BMPWIN定義
static const BMPWIN_DAT	bmplist2_dat[]={
	{
		GF_BGL_FRAME1_S, SEL2_PANEL0_X, SEL2_PANEL0_Y, SEL2_PANEL_W, SEL2_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET
	},
	{
		GF_BGL_FRAME1_S, SEL2_PANEL1_X, SEL2_PANEL1_Y, SEL2_PANEL_W, SEL2_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL2_PANEL_W*SEL2_PANEL_H*1,
	},
};
static const BMPWIN_DAT	bmplist3_dat[]={
	{
		GF_BGL_FRAME1_S, SEL3_PANEL0_X, SEL3_PANEL0_Y, SEL3_PANEL_W, SEL3_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL3_PANEL_W*SEL3_PANEL_H*0,
	},
	{
		GF_BGL_FRAME1_S, SEL3_PANEL1_X, SEL3_PANEL1_Y, SEL3_PANEL_W, SEL3_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL3_PANEL_W*SEL3_PANEL_H*1,
	},
	{
		GF_BGL_FRAME1_S, SEL3_PANEL2_X, SEL3_PANEL2_Y, SEL3_PANEL_W, SEL3_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL3_PANEL_W*SEL3_PANEL_H*2,
	},
};
static const BMPWIN_DAT	bmplist4_dat[]={
	{
		GF_BGL_FRAME1_S, SEL4_PANEL0_X, SEL4_PANEL0_Y, SEL4_PANEL_W, SEL4_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL4_PANEL_W*SEL4_PANEL_H*0,
	},
	{
		GF_BGL_FRAME1_S, SEL4_PANEL1_X, SEL4_PANEL1_Y, SEL4_PANEL_W, SEL4_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL4_PANEL_W*SEL4_PANEL_H*1,
	},
	{
		GF_BGL_FRAME1_S, SEL4_PANEL2_X, SEL4_PANEL2_Y, SEL4_PANEL_W, SEL4_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL4_PANEL_W*SEL4_PANEL_H*2,
	},
	{
		GF_BGL_FRAME1_S, SEL4_PANEL3_X, SEL4_PANEL3_Y, SEL4_PANEL_W, SEL4_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL4_PANEL_W*SEL4_PANEL_H*3,
	},

};
static const BMPWIN_DAT	bmplist5_dat[]={
	{
		GF_BGL_FRAME1_S, SEL5_PANEL0_X, SEL5_PANEL0_Y, SEL5_PANEL_W, SEL5_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL5_PANEL_W*SEL5_PANEL_H*0,
	},
	{
		GF_BGL_FRAME1_S, SEL5_PANEL1_X, SEL5_PANEL1_Y, SEL5_PANEL_W, SEL5_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL5_PANEL_W*SEL5_PANEL_H*1,
	},
	{
		GF_BGL_FRAME1_S, SEL5_PANEL2_X, SEL5_PANEL2_Y, SEL5_PANEL_W, SEL5_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL5_PANEL_W*SEL5_PANEL_H*2,
	},
	{
		GF_BGL_FRAME1_S, SEL5_PANEL3_X, SEL5_PANEL3_Y, SEL5_PANEL_W, SEL5_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL5_PANEL_W*SEL5_PANEL_H*3,
	},
	{
		GF_BGL_FRAME1_S, SEL5_PANEL4_X, SEL5_PANEL4_Y, SEL5_PANEL_W, SEL5_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL5_PANEL_W*SEL5_PANEL_H*4,
	},
};

static const BMPWIN_DAT	bmplist6_dat[]={
	{
		GF_BGL_FRAME1_S, SEL6_PANEL0_X, SEL6_PANEL0_Y, SEL6_PANEL_W, SEL6_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL6_PANEL_W*SEL6_PANEL_H*0,
	},
	{
		GF_BGL_FRAME1_S, SEL6_PANEL1_X, SEL6_PANEL0_Y, SEL6_PANEL_W, SEL6_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL6_PANEL_W*SEL6_PANEL_H*1,
	},
	{
		GF_BGL_FRAME1_S, SEL6_PANEL2_X, SEL6_PANEL2_Y, SEL6_PANEL_W, SEL6_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL6_PANEL_W*SEL6_PANEL_H*2,
	},
	{
		GF_BGL_FRAME1_S, SEL6_PANEL3_X, SEL6_PANEL3_Y, SEL6_PANEL_W, SEL6_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL6_PANEL_W*SEL6_PANEL_H*3,
	},
	{
		GF_BGL_FRAME1_S, SEL6_PANEL4_X, SEL6_PANEL4_Y, SEL6_PANEL_W, SEL6_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL6_PANEL_W*SEL6_PANEL_H*4,
	},
	{
		GF_BGL_FRAME1_S, SEL6_PANEL5_X, SEL6_PANEL5_Y, SEL6_PANEL_W, SEL6_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL6_PANEL_W*SEL6_PANEL_H*5,
	},
};

static const BMPWIN_DAT	bmplist7_dat[]={
	{
		GF_BGL_FRAME1_S, SEL7_PANEL0_X, SEL7_PANEL0_Y, SEL7_PANEL_W, SEL7_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL7_PANEL_W*SEL7_PANEL_H*0,
	},
	{
		GF_BGL_FRAME1_S, SEL7_PANEL1_X, SEL7_PANEL1_Y, SEL7_PANEL_W, SEL7_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL7_PANEL_W*SEL7_PANEL_H*1,
	},
	{
		GF_BGL_FRAME1_S, SEL7_PANEL2_X, SEL7_PANEL2_Y, SEL7_PANEL_W, SEL7_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL7_PANEL_W*SEL7_PANEL_H*2,
	},
	{
		GF_BGL_FRAME1_S, SEL7_PANEL3_X, SEL7_PANEL3_Y, SEL7_PANEL_W, SEL7_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL7_PANEL_W*SEL7_PANEL_H*3,
	},
	{
		GF_BGL_FRAME1_S, SEL7_PANEL4_X, SEL7_PANEL4_Y, SEL7_PANEL_W, SEL7_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL7_PANEL_W*SEL7_PANEL_H*4,
	},
	{
		GF_BGL_FRAME1_S, SEL7_PANEL5_X, SEL7_PANEL5_Y, SEL7_PANEL_W, SEL7_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL7_PANEL_W*SEL7_PANEL_H*5,
	},
	{
		GF_BGL_FRAME1_S, SEL7_PANEL6_X, SEL7_PANEL6_Y, SEL7_PANEL_W, SEL7_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL7_PANEL_W*SEL7_PANEL_H*6,
	},
};

static const BMPWIN_DAT	bmplist8_dat[]={
	{
		GF_BGL_FRAME1_S, SEL8_PANEL0_X, SEL8_PANEL0_Y, SEL8_PANEL_W, SEL8_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL8_PANEL_W*SEL8_PANEL_H*0,
	},
	{
		GF_BGL_FRAME1_S, SEL8_PANEL1_X, SEL8_PANEL1_Y, SEL8_PANEL_W, SEL8_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL8_PANEL_W*SEL8_PANEL_H*1,
	},
	{
		GF_BGL_FRAME1_S, SEL8_PANEL2_X, SEL8_PANEL2_Y, SEL8_PANEL_W, SEL8_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL8_PANEL_W*SEL8_PANEL_H*2,
	},
	{
		GF_BGL_FRAME1_S, SEL8_PANEL3_X, SEL8_PANEL3_Y, SEL8_PANEL_W, SEL8_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL8_PANEL_W*SEL8_PANEL_H*3,
	},
	{
		GF_BGL_FRAME1_S, SEL8_PANEL4_X, SEL8_PANEL4_Y, SEL8_PANEL_W, SEL8_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL8_PANEL_W*SEL8_PANEL_H*4,
	},
	{
		GF_BGL_FRAME1_S, SEL8_PANEL5_X, SEL8_PANEL5_Y, SEL8_PANEL_W, SEL8_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL8_PANEL_W*SEL8_PANEL_H*5,
	},
	{
		GF_BGL_FRAME1_S, SEL8_PANEL6_X, SEL8_PANEL6_Y, SEL8_PANEL_W, SEL8_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL8_PANEL_W*SEL8_PANEL_H*6,
	},
	{
		GF_BGL_FRAME1_S, SEL8_PANEL7_X, SEL8_PANEL7_Y, SEL8_PANEL_W, SEL8_PANEL_H,
		TALK_STRING_PAL, YESNO_BMP_OFFSET+	SEL8_PANEL_W*SEL8_PANEL_H*7,
	},
};

static const BMPWIN_DAT *bmplist_dat[]={
	bmplist2_dat,
	bmplist3_dat,
	bmplist4_dat,
	bmplist5_dat,
	bmplist6_dat,
	bmplist7_dat,
	bmplist8_dat,
};

//==============================================================================
/**
 * @brief   指定のパレット・キャラ・スクリーンを下画面用に転送
 *
 * @param   bgl
 *
 * @retval  none
 */
//==============================================================================
static void _bg_set( YESNO_SUBLCD_WORK *ysw )
{
	NNSG2dScreenData *scrnData;
	void *scrnBuf;
	// ファイルハンドルオープン
	ARCHANDLE* p_handle = ArchiveDataHandleOpen( ARC_SUBSELECT_GRA, HEAPID_POKETCH_APP );

	G2S_BlendNone();

	// BG0面の転送
    ArcUtil_HDL_PalSet( p_handle, NARC_subselect_bmpmenu_nclr, PALTYPE_SUB_BG,  0, 32*5, HEAPID_POKETCH_APP );

	// サブ画面BG2キャラ転送
	ArcUtil_HDL_BgCharSet( p_handle, NARC_subselect_bmpmenu_bg_ncgr, ysw->bgl,
							GF_BGL_FRAME2_S, 0, 0, 0, HEAPID_POKETCH_APP);

	// サブ画面BG2スクリーン転送
	ArcUtil_HDL_ScrnSet( p_handle, NARC_subselect_bmpmenu_back_nscr, ysw->bgl,GF_BGL_FRAME2_S, 0, 0, 0, HEAPID_POKETCH_APP);
//	ArcUtil_HDL_ScrnSet( p_handle, NARC_subselect_bmpmenu_back_nscr, ysw->bgl, GF_BGL_FRAME2_S, 0, u32 transSize, BOOL compressedFlag, u32 heapID);
	scrnBuf = ArcUtil_HDL_ScrnDataGet( p_handle, NARC_subselect_bmpmenu_back_nscr, 0, &scrnData, HEAPID_POKETCH_APP);
	GF_BGL_ScreenBufSet( ysw->bgl, GF_BGL_FRAME2_S, scrnData->rawData, scrnData->szByte );
	GF_BGL_LoadScreenV_Req( ysw->bgl, GF_BGL_FRAME2_S );

	sys_FreeMemoryEz(scrnBuf);

	// フォントパレット転送
//	SystemFontPaletteLoad( PALTYPE_SUB_BG, TALK_STRING_PAL*0x20, HEAPID_POKETCH_APP );

	// ファイルハンドルクローズ
	ArchiveDataHandleClose( p_handle );

}

//------------------------------------------------------------------
/**
 * @brief   はい・いいえ画面用BG初期化
 *
 * @param   ysw
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _yesno_bg_init( YESNO_SUBLCD_WORK *ysw )
{
	// ファイルハンドルオープン
	ARCHANDLE* p_handle = ArchiveDataHandleOpen( ARC_SUBSELECT_GRA, HEAPID_POKETCH_APP );

	// サブ画面BG1キャラ転送
	ArcUtil_HDL_BgCharSet( p_handle, NARC_subselect_bmpmenu_bg_ncgr, ysw->bgl,
							GF_BGL_FRAME0_S, 0, 0, 0, HEAPID_POKETCH_APP);

	// サブ画面BG1スクリーン転送
	ArcUtil_HDL_ScrnSet( p_handle, NARC_subselect_bmpmenu_yn_nscr, ysw->bgl,GF_BGL_FRAME0_S, 0, 0, 0, HEAPID_POKETCH_APP);

	// ファイルハンドルクローズ
	ArchiveDataHandleClose( p_handle );

	// BMPWIN確保「はい・いいえ」
	GF_BGL_BmpWinAdd( ysw->bgl, &ysw->YesnoBmpWin[0], GF_BGL_FRAME1_S,
					  YESNO_BMP_X, YESNO_BMP_Y1, YESNO_BMP_W, YESNO_BMP_H,//
					  TALK_STRING_PAL, YESNO_BMP_OFFSET	 );//

	GF_BGL_BmpWinAdd( ysw->bgl, &ysw->YesnoBmpWin[1], GF_BGL_FRAME1_S,
					  YESNO_BMP_X, YESNO_BMP_Y2, YESNO_BMP_W, YESNO_BMP_H,//
					  TALK_STRING_PAL, YESNO_BMP_OFFSET+YESNO_BMP_W*YESNO_BMP_H );//

	// 「はい・いいえ」描画
	ButtonMsgDraw( &ysw->YesnoBmpWin[0], ysw->msgman, msg_ev_win_046_01 );
	ButtonMsgDraw( &ysw->YesnoBmpWin[1], ysw->msgman, msg_ev_win_046_02 );


}



//------------------------------------------------------------------
/**
 * @brief   ＢＧ周り解放
 *
 * @param   ysw
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _yesno_bg_release( YESNO_SUBLCD_WORK *ysw )
{
	GF_BGL_ScrClear( ysw->bgl, GF_BGL_FRAME0_S );
	GF_BGL_ScrClear( ysw->bgl, GF_BGL_FRAME1_S );

	if( ysw->YesnoBmpWin[0].chrbuf != NULL ){
		GF_BGL_BmpWinDel( &ysw->YesnoBmpWin[0] );
	}
	if( ysw->YesnoBmpWin[1].chrbuf != NULL ){
		GF_BGL_BmpWinDel( &ysw->YesnoBmpWin[1] );
	}
}


//------------------------------------------------------------------
/**
 * @brief   はい・いいえ画面用BG初期化
 *
 * @param   ysw
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _selectlist_bg_init( YESNO_SUBLCD_WORK *ysw )
{
	int i;
	BMPLIST_DATA *list_dat = (BMPLIST_DATA *)EvWin_GetBmpListData( ysw->param.ev_win );
	int num = EvWin_GetBmpListNum( ysw->param.ev_win );
	// ファイルハンドルオープン
	ARCHANDLE* p_handle = ArchiveDataHandleOpen( ARC_SUBSELECT_GRA, HEAPID_POKETCH_APP );

	// サブ画面BG1キャラ転送
	ArcUtil_HDL_BgCharSet( p_handle, NARC_subselect_bmpmenu_bg_ncgr, ysw->bgl,
							GF_BGL_FRAME0_S, 0, 0, 0, HEAPID_POKETCH_APP);

	// サブ画面BG1スクリーン転送
	ArcUtil_HDL_ScrnSet( p_handle, NARC_subselect_bmpmenu_2_nscr+(num-2), ysw->bgl,GF_BGL_FRAME0_S, 0, 0, 0, HEAPID_POKETCH_APP);

	// ファイルハンドルクローズ
	ArchiveDataHandleClose( p_handle );


	for(i=0;i<num;i++){
		BMPWIN_DAT *tbl = (BMPWIN_DAT*)bmplist_dat[num-2];
		// BMPWIN確保「はい・いいえ」
		GF_BGL_BmpWinAddEx( ysw->bgl, &ysw->BmpMenuListWin[i], &tbl[i]	 );//
	}

	// 選択項目名描画
	for(i=0;i<num;i++){
//		ButtonMsgDraw( &ysw->BmpMenuListWin[i], ysw->msgman, msg_ev_win_046_01 );
		GF_BGL_BmpWinDataFill( &ysw->BmpMenuListWin[i],  0x0000 );
    //BTS1276対処 VBlankに変更
		GF_STR_PrintColor( &ysw->BmpMenuListWin[i], FONT_TOUCH, list_dat[i].str, 0, 0, MSG_NO_PUT, GF_PRINTCOLOR_MAKE( 2,1,0 ),NULL);
		GF_BGL_BmpWinOnVReq( &ysw->BmpMenuListWin[i] );
	}

	ysw->bmpmenulist_max = num;
}



//------------------------------------------------------------------
/**
 * @brief   ＢＧ周り解放
 *
 * @param   ysw
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _selectlist_bg_release( YESNO_SUBLCD_WORK *ysw )
{
	int num = EvWin_GetBmpListNum( ysw->param.ev_win );
	int i;

	GF_BGL_ScrClear( ysw->bgl, GF_BGL_FRAME0_S );
	GF_BGL_ScrClear( ysw->bgl, GF_BGL_FRAME1_S );

	for(i=0;i<num;i++){
		GF_BGL_BmpWinDel( &ysw->BmpMenuListWin[i] );
	}
}


//------------------------------------------------------------------
/**
 * @brief   セルアクター初期化
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _clact_init( YESNO_SUBLCD_WORK *ysw )
{
	int i;

	// セルアクター初期化
	ysw->clactSet = CLACT_U_SetEasyInit( MENU_ALL_OAM_NUM, &ysw->renddata, HEAPID_SUBLCD_MENU );

	CLACT_U_SetSubSurfaceMatrix( &ysw->renddata, 0, SUBLCD_MENU_SUB_ACTOR_DISTANCE );


	//リソースマネージャー初期化
	for(i=0;i<CLACT_RESOURCE_TYPE_NUM;i++){		//リソースマネージャー作成
		ysw->resMan[i] = CLACT_U_ResManagerInit(CLACT_RESOURCE_CHARA_NUM, i, HEAPID_SUBLCD_MENU);
	}


	//chara読み込み
	ysw->resObjTbl[CLACT_U_CHAR_RES] = CLACT_U_ResManagerResAddArcChar( ysw->resMan[CLACT_U_CHAR_RES],
									ARC_SUBSELECT_GRA, NARC_subselect_bmpmenu_obj_ncgr, 0,10,
									NNS_G2D_VRAM_TYPE_2DSUB, HEAPID_POKETCH_APP);
	//pal読み込み
	ysw->resObjTbl[CLACT_U_PLTT_RES] = CLACT_U_ResManagerResAddArcPltt( ysw->resMan[CLACT_U_PLTT_RES],
									ARC_SUBSELECT_GRA, NARC_subselect_bmpmenu_obj_nclr, 0,10,
									NNS_G2D_VRAM_TYPE_2DSUB, 1, HEAPID_POKETCH_APP);
	//cell読み込み
	ysw->resObjTbl[CLACT_U_CELL_RES] = CLACT_U_ResManagerResAddArcKindCell( ysw->resMan[CLACT_U_CELL_RES],
							ARC_SUBSELECT_GRA, NARC_subselect_bmpmenu_obj_ncer, 0,10, CLACT_U_CELL_RES, HEAPID_POKETCH_APP);

	//同じ関数でanim読み込み
	ysw->resObjTbl[CLACT_U_CELLANM_RES] = CLACT_U_ResManagerResAddArcKindCell( ysw->resMan[CLACT_U_CELLANM_RES],
							ARC_SUBSELECT_GRA, NARC_subselect_bmpmenu_obj_nanr, 0,10, CLACT_U_CELLANM_RES, HEAPID_POKETCH_APP );

	// リソースマネージャーから転送
	// Chara転送
	CLACT_U_CharManagerSetAreaCont( ysw->resObjTbl[CLACT_U_CHAR_RES] );
	// パレット転送
	CLACT_U_PlttManagerSetCleanArea( ysw->resObjTbl[CLACT_U_PLTT_RES] );

}

static const u16 yesno_cursor_pos_tbl[][2]={
	{ 128, 72 },
	{ 128, 120 },
};

//------------------------------------------------------------------
/**
 * @brief   セルアクター表示登録
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _clact_set(YESNO_SUBLCD_WORK *ysw, int pos)
{

	// セルアクターヘッダー生成
	CLACT_U_MakeHeader( &ysw->clActHeader,
						10, 10, 10, 10, CLACT_U_HEADER_DATA_NONE, CLACT_U_HEADER_DATA_NONE,
						0, 0,
						ysw->resMan[CLACT_U_CHAR_RES],
						ysw->resMan[CLACT_U_PLTT_RES],
						ysw->resMan[CLACT_U_CELL_RES],
						ysw->resMan[CLACT_U_CELLANM_RES],
						NULL,NULL);

	// セルアクター登録
	{
		//登録情報格納
		CLACT_ADD add;

		add.ClActSet	= ysw->clactSet;
		add.ClActHeader	= &ysw->clActHeader;

		add.mat.z		= 0;
		add.mat.x		= yesno_cursor_pos_tbl[pos][0]*FX32_ONE;
		add.mat.y		= (yesno_cursor_pos_tbl[pos][1]+SUBLCD_MENU_SUB_ACTOR_DOT)*FX32_ONE;
		add.sca.x		= FX32_ONE;
		add.sca.y		= FX32_ONE;
		add.sca.z		= FX32_ONE;
		add.rot			= 0;
		add.pri			= 1;
		add.DrawArea	= NNS_G2D_VRAM_TYPE_2DSUB;
		add.heap		= HEAPID_POKETCH_APP;

		ysw->CursorActWork = CLACT_Add(&add);
		CLACT_AnmChg( ysw->CursorActWork,1 );
		CLACT_SetAnmFlag( ysw->CursorActWork,1 );
		CLACT_SetDrawFlag( ysw->CursorActWork,0 );

	}
}



//------------------------------------------------------------------
/**
 * @brief   BMPWINにメッセージ描画
 *
 * @param   win
 * @param   msgman
 * @param   msgno
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void ButtonMsgDraw( GF_BGL_BMPWIN *win, MSGDATA_MANAGER *msgman, int msgno )
{
	STRBUF *strbuf = MSGMAN_AllocString( msgman, msgno );
	GF_BGL_BmpWinDataFill( win,  0x0000 );
	GF_STR_PrintColor( win, FONT_TOUCH, strbuf, 0, 0, MSG_NO_PUT, GF_PRINTCOLOR_MAKE( 2,1,0 ),NULL);
	GF_BGL_BmpWinOnVReq( win );

	STRBUF_Delete(strbuf);
}



//------------------------------------------------------------------
/**
 * @brief   文字列の取得準備
 *
 * @param   ysw
 * @param   flag	描画している面では文字描画の必要があるか？
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _string_get( YESNO_SUBLCD_WORK *ysw, int flag )
{

	// メッセージマネージャ初期化
	// 文字列マネージャー初期化

	ysw->WordSet    = WORDSET_Create( HEAPID_SUBLCD_MENU );
	ysw->msgman = MSGMAN_Create( MSGMAN_TYPE_NORMAL, ARC_MSG, NARC_msg_ev_win_dat, HEAPID_SUBLCD_MENU );

}

//------------------------------------------------------------------
/**
 * @brief   文字列マネージャーの解放
 *
 * @param   ysw
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _string_release( YESNO_SUBLCD_WORK *ysw )
{

	MSGMAN_Delete(  ysw->msgman  );
	WORDSET_Delete( ysw->WordSet );

}



//------------------------------------------------------------------
/**
 * @brief   はい・いいえ共通処理初期化
 *
 * @param   ysw
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _yesno_start( YESNO_SUBLCD_WORK *ysw )
{
	TOUCH_SW_PARAM tp_param;
	MI_CpuClear8(&tp_param,sizeof(TOUCH_SW_PARAM));

	// 初期化パラメータ設定
	tp_param.p_bgl 		= ysw->bgl;
	tp_param.bg_frame	= GF_BGL_FRAME2_S;
	tp_param.char_offs	= 1;
	tp_param.pltt_offs	= 12;
	tp_param.x			= 26;
	tp_param.y			= 10;
	tp_param.kt_st      = APP_KTST_KEY;
	tp_param.key_pos    = 0;			//yesがカレント

//	GF_BGL_ScrClear( ysw->bgl, GF_BGL_FRAME2_S );


}



//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
// サブシーケンス
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------



//------------------------------------------------------------------
/**
 * @brief   レポートにかきこみますか？画面作成
 *
 * @param   ysw
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int _seq_start( YESNO_SUBLCD_WORK *ysw )
{
//    ysw->riw = ReportSubLcd_Create(ysw->fsys, HEAPID_FIELD, GF_BGL_FRAME1_S);
// 	ReportSubLcd_WriteWindow(ysw->riw);

	ysw->param.subseq = SEQ_WAIT;


	return FALSE;
}


//------------------------------------------------------------------
/**
 * @brief   レポートを書きますか？のはい・いいえ開始
 *
 * @param   ysw
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int _seq_yesno_start( YESNO_SUBLCD_WORK *ysw )
{

//	_yesno_start( ysw );
	ysw->pos = 0;
	_yesno_bg_init( ysw );
	CLACT_SetDrawFlag( ysw->CursorActWork, 1 );
	cursor_act_pos( ysw, ysw->pos );
	CLACT_AnmChg( ysw->CursorActWork, 1 );

	ysw->param.subseq = SEQ_YESNO_WAIT;
	return FALSE;

}

//------------------------------------------------------------------
/**
 * @brief   レポートを書きますか？のはい・いいえ待ち
 *
 * @param   ysw
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int _seq_yesno_wait( YESNO_SUBLCD_WORK *ysw )
{
	yesno_control( ysw );

/*
	switch(result){
	case TOUCH_SW_RET_YES: case TOUCH_SW_RET_YES_TOUCH:

		// 決定時の方法を伝える
		_set_decide_style( &ysw->fsys->KeyTouchStatus, result );

		ysw->param.subseq = SEQ_END;
		break;
	case TOUCH_SW_RET_NO: case TOUCH_SW_RET_NO_TOUCH:

		// 決定時の方法を伝える
		_set_decide_style( &ysw->fsys->KeyTouchStatus, result );

//		ysw->param.subseq = SEQ_REPORT_END;
		return TRUE;
		break;
	}
*/

	return FALSE;

}


//------------------------------------------------------------------
/**
 * @brief   はい・いいえ終了処理
 *
 * @param   ysw
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int _seq_yesno_anime_wait( YESNO_SUBLCD_WORK *ysw )
{
	// カーソルアニメ終了待ち
	if(CLACT_AnmActiveCheck( ysw->CursorActWork )==FALSE){
		CLACT_SetDrawFlag( ysw->CursorActWork, 0);
		_yesno_bg_release(ysw);
		// 結果格納
		*((u16*)(ysw->param.scr_work)) = ysw->pos;

		ysw->param.subseq = SEQ_YESNO_END;
	}

	return FALSE;
}


static int _seq_yesno_end( YESNO_SUBLCD_WORK *ysw )
{
	CLACT_SetDrawFlag( ysw->CursorActWork, 0);

	ysw->param.subseq = SEQ_WAIT;
	return FALSE;

}
//------------------------------------------------------------------
/**
 * @brief   レポート処理終了（エラー・正常・途中終了も含む）
 *
 * @param   ysw
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int _seq_end( YESNO_SUBLCD_WORK *ysw )
{
	if(++ysw->wait > REPORT_END_WAIT){
		return TRUE;
	}

	return FALSE;
}

//------------------------------------------------------------------
/**
 * @brief   選択システム待機状態
 *
 * @param   ysw
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int _seq_wait( YESNO_SUBLCD_WORK *ysw )
{
	// 待機状態。外部からシーケンスが変更されるのを待っている
	if(sys.tp_cont){
		sys.extra_cont = PAD_BUTTON_A;
	}

//	OS_Printf("選択肢システム待機状態\n");

	return FALSE;
}

//------------------------------------------------------------------
/**
 * @brief   リスト選択初期化
 *
 * @param   ysw
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int _seq_selectlist_start( YESNO_SUBLCD_WORK *ysw )
{
	ysw->param.subseq = SEQ_SELECTLIST_WAIT;
	ysw->pos = 0;

	_selectlist_bg_init( ysw );
	CLACT_SetDrawFlag( ysw->CursorActWork, 1 );
	bmpmenu_cursor_act_pos( ysw, ysw->bmpmenulist_max, ysw->pos );
	if( ysw->callback_func != NULL ){
		ysw->callback_func( ysw->callback_work, ysw->pos );
	}
	*((u16*)(ysw->param.scr_work)) = EV_WIN_NOTHING;



	return FALSE;
}


static const RECT_HIT_TBL touch_bmplist2_tbl[]={
 		{ 50,  92, 3, 251, },
		{ 99, 140, 3, 251, },
 		{TP_HIT_END,0,0,0},		// 終了データ
};
static const RECT_HIT_TBL touch_bmplist3_tbl[]={
 		{ 27,  68, 3, 251, },
		{ 74, 115, 3, 251, },
		{123, 164, 3, 251, },
 		{TP_HIT_END,0,0,0},		// 終了データ
};
static const RECT_HIT_TBL touch_bmplist4_tbl[]={
 		{  2,  43, 3, 251, },
		{ 52,  92, 3, 251, },
		{ 99, 140, 3, 251, },
		{148, 188, 3, 251, },
 		{TP_HIT_END,0,0,0},		// 終了データ
};
static const RECT_HIT_TBL touch_bmplist5_tbl[]={
 		{ 25,  68, 3, 123, },	{ 25,  68, 131, 252, },
 		{ 75, 115, 3, 123, },	{ 75, 115, 131, 252, },
								{123, 163, 131, 252, },
 		{TP_HIT_END,0,0,0},		// 終了データ
};
static const RECT_HIT_TBL touch_bmplist6_tbl[]={
 		{ 25,  68, 3, 123, },	{ 25,  68, 131, 252, },
 		{ 75, 115, 3, 123, },	{ 75, 115, 131, 252, },
		{123, 163, 3, 123, }, 	{123, 163, 131, 252, },
 		{TP_HIT_END,0,0,0},		// 終了データ
};
static const RECT_HIT_TBL touch_bmplist7_tbl[]={
 		{  3,  44, 3, 123, },	{  3,  44, 131, 252, },
 		{ 51,  91, 3, 123, },	{ 51,  91, 131, 252, },
		{100, 139, 3, 123, }, 	{100, 139, 131, 252, },
								{147, 188, 131, 252, },
 		{TP_HIT_END,0,0,0},		// 終了データ
};
static const RECT_HIT_TBL touch_bmplist8_tbl[]={
 		{  3,  44, 3, 123, },	{  3,  44, 131, 252, },
 		{ 51,  91, 3, 123, },	{ 51,  91, 131, 252, },
		{100, 139, 3, 123, }, 	{100, 139, 131, 252, },
		{147, 188, 3, 123, },	{147, 188, 131, 252, },
 		{TP_HIT_END,0,0,0},		// 終了データ
};

static const RECT_HIT_TBL *touch_bmplist_tbl[]={
	touch_bmplist2_tbl,
	touch_bmplist3_tbl,
	touch_bmplist4_tbl,
	touch_bmplist5_tbl,
	touch_bmplist6_tbl,
	touch_bmplist7_tbl,
	touch_bmplist8_tbl,
};


// up, down, left, rightの順
typedef struct{
	s8 arrow[4];
}MOVE_POINT;
static const MOVE_POINT key_tbl_2[]={
	{0,1,-1,-1,},
	{0,1,-1,-1,},
};

static const MOVE_POINT key_tbl_3[]={
	{0,1, -1, -1,},
	{0,2, -1, -1,},
	{1,2, -1, -1,},
};

static const MOVE_POINT key_tbl_4[]={
	{ 0, 1,  -1, -1, },
	{ 0, 2,  -1, -1, },
	{ 1, 3,  -1, -1, },
	{ 2, 3,  -1, -1, },
};

static const MOVE_POINT key_tbl_5[]={
	{0,2,  0,  1, }, { 1, 3, 0, 1,},
	{0,4,  2,  3, }, { 1, 4, 2, 3,},
					 { 3, 4, -1, -1,},
};

static const MOVE_POINT key_tbl_6[]={
	{0,2,  0,  1, }, { 0, 3, 0, 1,},
	{0,4,  2,  3, }, { 1, 5, 2, 3,},
	{2,4,  4,  5, }, { 3, 5, 4, 5,},
};

static const MOVE_POINT key_tbl_7[]={
	{0,2,  0,  1, }, { 1, 3, 0, 1,},
	{0,4,  2,  3, }, { 1, 5, 2, 3,},
	{2,6,  4,  5, }, { 3, 6, 4, 5,},
					 { 5, 6, -1, -1,},
};
static const MOVE_POINT key_tbl_8[]={
	{0,2,  0,  1, }, { 0, 3, 0, 1,},
	{0,4,  2,  3, }, { 1, 5, 2, 3,},
	{2,6,  4,  5, }, { 3, 7, 4, 5,},
	{4,6,  6,  7, }, { 5, 7, 6, 7,},
};

static const MOVE_POINT *key_tbl_28[]={
	key_tbl_2,
	key_tbl_3,
	key_tbl_4,
	key_tbl_5,
	key_tbl_6,
	key_tbl_7,
	key_tbl_8,
};


//------------------------------------------------------------------
/**
 * @brief   キー操作の結果、どこにカーソルが移動するか
 *
 * @param   pos
 * @param   max
 * @param   arrow
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int move_key_next( int *pos, int max, int arrow )
{
	int index = max-2;
	int next;
	MOVE_POINT *tbl = (MOVE_POINT*)key_tbl_28[index];

	// -1か同じ場所だったら更新しない
	next = tbl[*pos].arrow[arrow];
	if(next==-1 || *pos == next){
		return 0;
	}

	// 更新
	*pos = next;
	return 1;
}

enum{
	MOVE_UP=0,
	MOVE_DOWN,
	MOVE_LEFT,
	MOVE_RIGHT,
};

//------------------------------------------------------------------
/**
 * @brief   リスト選択待ち＆終了
 *
 * @param   ysw
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int _seq_selectlist_wait( YESNO_SUBLCD_WORK *ysw )
{
	RECT_HIT_TBL *touch_tbl = (RECT_HIT_TBL *)touch_bmplist_tbl[ysw->bmpmenulist_max-2];
	u32 ret=GF_TP_RectHitTrg( touch_tbl );
	if(ret!=RECT_HIT_NONE){
		ysw->pos = ret;
		bmpmenu_cursor_act_pos( ysw, ysw->bmpmenulist_max, ysw->pos );
		Snd_SePlay( SEQ_SE_DP_SELECT );
		ysw->param.subseq = SEQ_SELECTLIST_ANIME_WAIT;
		bmpmenu_cursor_anime_start( ysw, ysw->bmpmenulist_max );
		if( ysw->callback_func != NULL ){
			ysw->callback_func( ysw->callback_work, ysw->pos );
		}
//		MORI_PRINT("num=%d,touch=%d", ysw->bmpmenulist_max, ret );
	}else{
		if(sys.trg&PAD_KEY_UP){
			if(move_key_next( &ysw->pos, ysw->bmpmenulist_max, MOVE_UP )){
				bmpmenu_cursor_act_pos( ysw, ysw->bmpmenulist_max, ysw->pos );
				Snd_SePlay( SEQ_SE_DP_SELECT );
				if( ysw->callback_func != NULL ){
					ysw->callback_func( ysw->callback_work, ysw->pos );
				}
			}
		}else if(sys.trg&PAD_KEY_DOWN){
			if(move_key_next( &ysw->pos, ysw->bmpmenulist_max, MOVE_DOWN )){
				bmpmenu_cursor_act_pos( ysw, ysw->bmpmenulist_max, ysw->pos );
				Snd_SePlay( SEQ_SE_DP_SELECT );
				if( ysw->callback_func != NULL ){
					ysw->callback_func( ysw->callback_work, ysw->pos );
				}
			}
		}else if(sys.trg&PAD_KEY_LEFT){
			if(move_key_next( &ysw->pos, ysw->bmpmenulist_max, MOVE_LEFT )){
				bmpmenu_cursor_act_pos( ysw, ysw->bmpmenulist_max, ysw->pos );
				Snd_SePlay( SEQ_SE_DP_SELECT );
				if( ysw->callback_func != NULL ){
					ysw->callback_func( ysw->callback_work, ysw->pos );
				}
			}
		}else if(sys.trg&PAD_KEY_RIGHT){
			if(move_key_next( &ysw->pos, ysw->bmpmenulist_max, MOVE_RIGHT )){
				bmpmenu_cursor_act_pos( ysw, ysw->bmpmenulist_max, ysw->pos );
				Snd_SePlay( SEQ_SE_DP_SELECT );
				if( ysw->callback_func != NULL ){
					ysw->callback_func( ysw->callback_work, ysw->pos );
				}
			}
		}else if(sys.trg & PAD_BUTTON_DECIDE){
			bmpmenu_cursor_anime_start( ysw, ysw->bmpmenulist_max );
			ysw->param.subseq = SEQ_SELECTLIST_ANIME_WAIT;
			Snd_SePlay( SEQ_SE_DP_SELECT );
		}else if(sys.trg & PAD_BUTTON_CANCEL){
			// キャンセルフラグが設定されていたら反応する
			if(EvWin_GetCancelFlag(ysw->param.ev_win)){
				// Bボタンキャンセルを押すと一番下の選択肢を選択した事にしている
				ysw->pos = ysw->bmpmenulist_max-1;
				bmpmenu_cursor_act_pos( ysw, ysw->bmpmenulist_max, ysw->pos );
				bmpmenu_cursor_anime_start( ysw, ysw->bmpmenulist_max );
				ysw->param.subseq = SEQ_SELECTLIST_ANIME_WAIT;
				Snd_SePlay( SEQ_SE_DP_SELECT );
			}
		}
	}
	return FALSE;
}


//------------------------------------------------------------------
/**
 * @brief
 *
 * @param   ysw
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int _seq_selectlist_anime_wait( YESNO_SUBLCD_WORK *ysw )
{
//	BMPLIST_DATA *list_dat = (BMPLIST_DATA *)EvWin_GetBmpListData( ysw->param.ev_win );
	if(CLACT_AnmActiveCheck( ysw->CursorActWork )==FALSE){
		CLACT_SetDrawFlag( ysw->CursorActWork, 0);
		_selectlist_bg_release( ysw );
		ysw->param.subseq = SEQ_SELECTLIST_END;

	}
	return FALSE;
}


//------------------------------------------------------------------
/**
 * @brief   選択リスト終了処理
 *
 * @param   ysw
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int _seq_selectlist_end( YESNO_SUBLCD_WORK *ysw )
{
	BMPLIST_DATA *list_dat = (BMPLIST_DATA *)EvWin_GetBmpListData( ysw->param.ev_win );

	*((u16*)(ysw->param.scr_work)) = list_dat[ysw->pos].param;
	ysw->param.subseq = SEQ_WAIT;
	return FALSE;
}

//------------------------------------------------------------------
/**
 * @brief   はい・いいえを選択したときの決定方法を渡す
 *
 * @param   kt_st
 * @param   type
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void _set_decide_style( KEYTOUCH_STATUS *kt_st, int type )
{
	if( type==TOUCH_SW_RET_YES|| type==TOUCH_SW_RET_NO){

		KeyTouchStatus_SetTouchOrKey( kt_st, APP_END_KEY );

	}else if(type==TOUCH_SW_RET_YES_TOUCH||type==TOUCH_SW_RET_NO_TOUCH){

		KeyTouchStatus_SetTouchOrKey( kt_st, APP_END_TOUCH );

	}
}

//------------------------------------------------------------------
/**
 * @brief   カーソル座標設定
 *
 * @param   wk
 * @param   pos
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void cursor_act_pos( YESNO_SUBLCD_WORK *ysw, int pos )
{
	VecFx32 mat;
	mat.x = yesno_cursor_pos_tbl[pos][0]*FX32_ONE;
	mat.y = (yesno_cursor_pos_tbl[pos][1]+SUBLCD_MENU_SUB_ACTOR_DOT)*FX32_ONE;
	mat.z = 0;

	CLACT_SetMatrix( ysw->CursorActWork, &mat );

}

static const u8 sel_dat[][8][3]={
	// 2個
	{
		{ 128,  72, 1},		{ 128, 120, 1},
	},

	// 3個
	{
		{ 128,  48, 1},		{ 128,  96, 1},		{ 128, 144, 1},
	},

	// 4個
	{
		{ 128,  24, 1},		{ 128,  72, 1},		{ 128, 120, 1},		{ 128, 168, 1},
	},

	// 5個
	{
		{  64,  48, 0},		{ 192,  48, 0},
		{  64,  96, 0},		{ 192,  96, 0},
							{ 192, 144, 0},
	},

	// 6個
	{
		{  64,  48, 0},		{ 192,  48, 0},
		{  64,  96, 0},		{ 192,  96, 0},
		{  64, 144, 0},		{ 192, 144, 0},
	},

	// 7個
	{
		{  64,  24, 0},		{ 192,  24, 0},
		{  64,  72, 0},		{ 192,  72, 0},
		{  64, 120, 0},		{ 192, 120, 0},
							{ 192, 168, 0},
	},

	// 8個
	{
		{  64,  24, 0},		{ 192,  24, 0},
		{  64,  72, 0},		{ 192,  72, 0},
		{  64, 120, 0},		{ 192, 120, 0},
		{  64, 168, 0},		{ 192, 168, 0},
	},

};

//------------------------------------------------------------------
/**
 * @brief   BMPMENU形状時のカーソル移動ルーチン
 *
 * @param   ysw		YESNO_SUBLCD_WORK
 * @param   max		選択リストの項目数
 * @param   pos		現在の場所番号
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void bmpmenu_cursor_act_pos( YESNO_SUBLCD_WORK *ysw, int max, int pos )
{
	VecFx32 mat;
	mat.x = sel_dat[max-2][pos][0]*FX32_ONE;
	mat.y = (sel_dat[max-2][pos][1]+SUBLCD_MENU_SUB_ACTOR_DOT)*FX32_ONE;
	mat.z = 0;

	CLACT_AnmChg( ysw->CursorActWork, sel_dat[max-2][pos][2] );
	CLACT_SetMatrix( ysw->CursorActWork, &mat );
}

static const RECT_HIT_TBL yesno_touch_tbl[]={
 	{ 50,  92, 3, 251, },
	{ 99, 140, 3, 251, },
 	{TP_HIT_END,0,0,0},		// 終了データ

};

//------------------------------------------------------------------
/**
 * @brief   決定時のカーソルのアニメーション番号を指定する
 *
 * @param   ysw
 * @param   max
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void bmpmenu_cursor_anime_start( YESNO_SUBLCD_WORK *ysw, int max )
{
	CLACT_AnmChg( ysw->CursorActWork, sel_dat[max-2][0][2]+2 );
}

//------------------------------------------------------------------
/**
 * @brief   YESNO選択待ち（シーケンス関数）
 *
 * @param   wk
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int yesno_control( YESNO_SUBLCD_WORK *ysw )
{
	u32 ret=GF_TP_RectHitTrg( yesno_touch_tbl );
	if(ret!=RECT_HIT_NONE){
		if(ret==0){
			ysw->pos = 0;
			cursor_act_pos( ysw, ysw->pos );
			CLACT_AnmChg( ysw->CursorActWork, 3 );
			Snd_SePlay( SEQ_SE_DP_SELECT );
			ysw->param.subseq = SEQ_YESNO_ANIME_WAIT;

		}else if(ret==1){
			ysw->pos = 1;
			cursor_act_pos( ysw, ysw->pos );
			CLACT_AnmChg( ysw->CursorActWork, 3 );
			Snd_SePlay( SEQ_SE_DP_SELECT );
			ysw->param.subseq = SEQ_YESNO_ANIME_WAIT;
		}
	}else{
		if(sys.trg&PAD_KEY_UP){
			if(ysw->pos==1){
				Snd_SePlay( SEQ_SE_DP_SELECT );
			}
			ysw->pos = 0;
			cursor_act_pos( ysw, ysw->pos );
		}else if(sys.trg&PAD_KEY_DOWN){
			if(ysw->pos==0){
				Snd_SePlay( SEQ_SE_DP_SELECT );
			}
			ysw->pos = 1;
			cursor_act_pos( ysw, ysw->pos );
		}else if(sys.trg&PAD_BUTTON_DECIDE){
			CLACT_AnmChg( ysw->CursorActWork, 3 );
			ysw->param.subseq = SEQ_YESNO_ANIME_WAIT;
			Snd_SePlay( SEQ_SE_DP_SELECT );
		}else if(sys.trg&PAD_BUTTON_CANCEL){
			ysw->pos = 1;
			cursor_act_pos( ysw, ysw->pos );
			CLACT_AnmChg( ysw->CursorActWork, 3 );
			Snd_SePlay( SEQ_SE_DP_SELECT );
			ysw->param.subseq = SEQ_YESNO_ANIME_WAIT;
		}
	}

	return YESNO_CONTINUE;
}

// 強制終了
static int _seq_break( YESNO_SUBLCD_WORK *ysw )
{
	_yesno_bg_release(ysw);
	return _seq_yesno_end( ysw );
}